<?php
namespace app\madmin\model;
use think\Model;

/**
 * 日志模型类
 * @author  黄明杰 vx:HMJ125855
 * @version 20190101
 */
class SystemLogModel extends Model
{

    protected $name = 'SystemLog';

    /**
     * 数据统计方法
     * @param   Array   $Where      查询条件
     * @param   String  $Field      查询字段
     * @param   Int     $Group      分组查询
     * @param   String  $Order      查询分页
     * @return  Array
     */
    public function mCensus($Where = [], $Field = '*', $Group = '', $Order = ''){
        return $this->field($Field)->where($Where)->group($Group)->order($Order)->select();
    }

    /**
     * 数据列表方法
     * @param   Array   $Where  查询条件
     * @param   String  $Field  查询字段
     * @param   Int     $Limit  每页条数
     * @param   Srting  $Order  查询排序
     * @return  Array
     */
    public function mList($Where = [], $Field = '*', $Limit = 15, $Order = 'log_id DESC'){
        $Count = $this->mCount($Where);

        $Where['log_del'] = 1;

        return $this->field($Field)->join('db_system_member', 'db_system_log.log_member = db_system_member.member_id', 'LEFT')->where($Where)->order($Order)->paginate($Limit, $Count, ['query'=>request()->param()])->each(function($k, $v){
            $k->log_create = date('Y/m/d H:i:s', $k->log_create);
        });
    }

    /**
     * 数据统计方法
     * @param   Array   $Where  查询条件
     * @return  Int
     */
    public function mCount($Where = []){
        $Where['log_del'] = 1;

        return $this->where($Where)->count();
    }

    /**
     * 数据详情方法
     * @param   Array   $Where  查询条件
     * @param   String  $Field  查询字段
     * @return  Array
     */
    public function mInfo($Where = [], $Field = '*'){
        $Where['log_del'] = 1;

        return $this->field($Field)->where($Where)->find();
    }

    /**
     * 数据详情方法
     * @param   Array   $Where  查询条件
     * @param   String  $Field  查询字段
     * @return  String
     */
    public function mField($Where, $Field){
        $Where['log_del'] = 1;

        return $this->where($Where)->value($Field);
    }

    /**
     * 数据删除方法
     * @param   Array   $Where  更新条件
     * @return  Array
     */
    public function mDel($Where){
        $Code = 2;
        $Time = time();

        try{
            $Where['log_del'] = 1;

            $Param = [];
            $Param['log_del'] = 2;
            $Param['log_update'] = $Time;

            $Result = $this->allowField(true)->where($Where)->update($Param);

            if($Result){
                $Code = 1;
                $Msgs = lang('XtTsScCs');
            }else{
                $Msgs = lang('XtTsScSb');
            }

            return ['code'=>$Code, 'msgs'=>$Msgs];
        }catch(PDOException $e){
            return ['code'=>$Code, 'msg'=>$e->getMessage()];
        }
    }

}